clear;clc;
%% create session, connect devices
session=MeasurementSession.SimpleMeasurement([Constant.AutoMeasurementRootPath '/example/ParametricMagnetometer/device.dat']);
% session.getAllDevicesInBase('session');

%% add parameters
session.addParameter('CellTemperature', 90.0, 'degreeC');
session.addParameter('PumpControlVolt', 5.0, 'V');
session.addParameter('ProbPower', 2.0, 'mW');
session.addParameter('X1CoilControlVolt', 0.0, 'V');
session.addParameter('X9CoilControlOffset', 3.0, 'V');
session.addParameter('Z1CoilControlVolt', 10e-3, 'V');

%% Device Settings
aom = session.getDevice('aomPump');

zi = session.getDevice('dev3543');
zi.setAuxOutOffset('0', session.getParameter('X1CoilControlVolt') );
zi.setAuxOutOffset('1', session.getParameter('Z1CoilControlVolt') );
zi.setSigOutOffset( session.getParameter('X9CoilControlOffset')  );
zi.setSigOutImpedanceInf().fireSigOut();

sweeper = zi.sweepModule();
sweeper.setControl('gridnode', 'oscs/0/freq', ...
                   'xmapping', 0, 'start', 10e3, 'stop', 80e3)...
       .setFilter('bandwidthcontrol', 0)...
       .setSettling('settling_tc', 1)...
       .setStatistics('averaging_sample', 100)...
       .setOption()...
       .showParam();

%% single sweep 
% sweeper.subscribeChannel('0');
% res = sweeper.run('processMonitor', @plotData);
% sweeper.unsubscribeChannel('0');

%% batch sweep 
session.addParameter('scanX9CoilAmplitude', 0:0.1:2.5, 'V');
session.makeLoopIndex('scanX9CoilAmplitude');

sweeper.subscribeChannel('0');
result = sweeper.batchSweep_Session(@batch_fun, session, 'processMonitor', @plotData);
sweeper.unsubscribeChannel('0');

post_plot(result)
%%
function plotData(sample, index)
    plot(sample.grid, sample.r);
    xlim(minmax(sample.grid));
    title(sprintf('loop index = %d', index));
end

function post_plot(result)
    freq = result{1}.dev3543.demods.sample{1}.frequency;
    amp = 0:0.1:2.5;
    [x, y] = meshgrid(freq, amp);
    f = @(x) x.dev3543.demods.sample{1}.r;
    data=reshape(cell2mat(cellfun(f, result, 'UniformOutput', false)), [100, 26]).';
    contourf(x, y, data, 20)
end

%% Operations of Each Sweep
function res = batch_fun(sweeper, index, session, varargin)
    %  get Parameters
    p=inputParser;
    isSwpr = @(x) isa(x, 'EquipmentControl.AnalyseEquipment.ZI.ziSweeper');
    isSess = @(x) isa(x, 'MeasurementSession.AbstractMeasurementSession');
    
    p.addRequired('sweeper', isSwpr );
    p.addRequired('index', @isnumeric);
    p.addRequired('session',  isSess);
    p.addParameter('processMonitor', @plotData);
    p.parse(sweeper, index, session, varargin{:});

    swpr = p.Results.sweeper;
    idx = p.Results.index;
    sess = p.Results.session;
    
    updataPlot = p.Results.processMonitor;
    
    %  change settings (e.g., laser voltage) for each parameter sweeping.
    zi = sess.getDevice('dev3543');
    amp = sess.getParameter('scanX9CoilAmplitude');
    zi.setSigOutAmplitude( '0', amp(idx) );
    fprintf('\t Sweeping at paramter: %f ... \n', amp(idx));
    pause(1);

    % do the sweep
    res = swpr.run('index', idx, 'silence', 0, 'processMonitor', updataPlot);

end